
clear
set more off

/*********************************************************************************
Name: detections.do

Data In: [Data/Original/patient_surveys_attempts.dta,
		  Data/Original/patient_surveys.dta,
		  Data/Original/tb_registers.dta,
		  Data/Original/patient_start_dates.dta,
		  Data/Original/hw_roster.dta,
		  Data/Intermediate/verified_patients.dta,
		  Data/Intermediate/hw_roster_by_month.dta,
		  Data/Intermediate/hw_controls.dta,
		  Data/Original/centerwise_sheets.dta]

Data Out: [Data/Intermediate/Table7_PanelA_1_input.dta,
		   Data/Intermediate/Table7_PanelA_2_input.dta]

Results Out: [Results/Appendix/TableA10_PanelA.out,
			  Results/Paper/Table7_PanelB.out,
			  Results/Appendix/TableC5_PanelB.log,
			  Results/Appendix/TableD5_PanelB.log,
			  Results/Appendix/TableA10_PanelB.out,
			  Results/Paper/Table7_PanelC.out,
			  Results/Appendix/TableC5_PanelC.log,
			  Results/Appendix/TableD5_PanelC.log,
			  Results/Paper/Table5_PanelA.out,
			  Results/Appendix/TableC3_PanelA.log,
			  Results/Appendix/TableD3_PanelA.log,
			  Results/Appendix/TableE3.out,
			  Results/Paper/Table7_PanelA.out,
			  Results/Appendix/TableC5_PanelA.log,
			  Results/Appendix/TableD5_PanelA.log,
			  Results/Appendix/TableE5.out]

Purpose of do-file: Estimating the treatment's impact on detections and on the quality of reporting on detections

Organization: PART-1: Estimating the treatment's impact on the quality of reporting on patient detection, individual-level analysis
			  PART-2: Estimating the treatment's impact on the true number of detections
			  PART-3: Estimating the treatment's impact on the quality of reporting on patient detection, center-level analysis
*********************************************************************************/

* Setting path directory
cd "${DIRECTORY}"


****************************************
*** PART-1 *** Estimating the treatment's impact on the quality of reporting on patient detection, individual-level analysis
****************************************

** Calling and merging datasets

use "Data/Original/patient_surveys_attempts.dta", clear

merge 1:1 q1_ques_code a02_entry_exit using "Data/Original/patient_surveys.dta"
drop _merge

merge m:1 q1_ques_code using "Data/Original/tb_registers.dta"
drop if _merge == 2
drop _merge

merge m:1 q1_ques_code using "Data/Original/patient_start_dates.dta"
keep if _merge == 3
drop _merge

merge m:1 Unique_ID UID_Center using "Data/Original/hw_roster.dta"
keep if _merge == 3
drop _merge

merge 1:1 q1_ques_code a02_entry_exit using "Data/Intermediate/verified_patients.dta"
keep if _merge == 3
drop _merge


** Creating the variables indicating reason for non-completion
// We consider any patient surveyed successfully at least once as verified.
// For patients not verified, we consider the outcome of their first survey

gen notcompl_refused = (q14_outcome_survey == 3 | q14_outcome_survey == 6) & verified_patient ~= 1 if q14_outcome_survey ~= .
gen notcompl_notfound = (q14_outcome_survey == 4 | q14_outcome_survey == 7) & verified_patient ~= 1 if q14_outcome_survey ~= .
gen notcompl_counswarning = (q14_outcome_survey == 8) & verified_patient ~= 1 if q14_outcome_survey ~= .
gen notcompl_died = (q14_outcome_survey == 9) & verified_patient ~= 1 if q14_outcome_survey ~= .
gen notcompl_other = (q14_outcome_survey == 5) & verified_patient ~= 1 if q14_outcome_survey ~= .


** Impact on survey outcomes

* Baseline regressions for patients who started the treatment before the start of the experiment: Table A10, Panel A

su verified_patient if treatment == 0 & post_exp == 0 & first_attempt == 1
local mean_control=r(mean)
ivreg2 verified_patient treatment final_stratum_id1-final_stratum_id13 entry_survey if post_exp == 0 & first_attempt == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableA10_PanelA.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

foreach var in notcompl_refused notcompl_notfound notcompl_counswarning notcompl_died notcompl_other{

	su `var' if treatment == 0 & post_exp == 0 & first_attempt == 1	
	local mean_control=r(mean)
	ivreg2 `var' treatment final_stratum_id1-final_stratum_id13 entry_survey if post_exp == 0 & first_attempt == 1, small cl(uid_cluster)   
	quietly outreg2 treatment using "Results/Appendix/TableA10_PanelA.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

}

* Impact regressions for patients who started the treatment after the start of the experiment: Table 7, Panel B

* Sidak-Holm correction

foreach y in verified_patient notcompl_refused notcompl_notfound notcompl_counswarning notcompl_died notcompl_other{
	ivreg2 `y' treatment final_stratum_id1-final_stratum_id13 entry_survey if post_exp == 1 & first_attempt == 1, small cl(uid_cluster)   
	test treatment
	gen `y'_p1 = `r(p)'
}	

preserve

keep verified_patient_p* notcompl_refused_p* notcompl_notfound_p* notcompl_counswarning_p* notcompl_died_p* notcompl_other_p*
gen id = 99
keep in 1
reshape long verified_patient_p notcompl_refused_p notcompl_notfound_p notcompl_counswarning_p notcompl_died_p notcompl_other_p, i(id) j(test) 
drop id
xpose, clear varname
drop in 1
rename v1 pval_1
rename _varname variable	
gen number = _N
foreach y in  pval_1 {
	sort `y'
	gen `y'_k=(_N+1)-_n	
	generate `y'_sidak=1-(1-`y')^`y'_k
	replace  `y'_sidak=`y'_sidak[_n-1] if `y'_sidak[_n-1]>`y'_sidak in 2/L
	replace  `y'_sidak=1 if `y'_sidak>1 & `y'_sidak~=.
	drop `y'_k
}
drop number
order variable pval_1*

foreach y in verified_patient_p notcompl_refused_p notcompl_notfound_p notcompl_counswarning_p notcompl_died_p notcompl_other_p {
	sum pval_1_sidak if variable=="`y'"
	local `y'1 = string(r(mean))
}

restore

su verified_patient if treatment == 0 & post_exp == 1 & first_attempt == 1	
local mean_control=r(mean)
ivreg2 verified_patient treatment final_stratum_id1-final_stratum_id13 entry_survey if post_exp == 1 & first_attempt == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table7_PanelB.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control', "Sidak-Holm p-val", `verified_patient_p1') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

foreach var in notcompl_refused notcompl_notfound notcompl_counswarning notcompl_died notcompl_other{

	su `var' if treatment == 0 & post_exp == 1 & first_attempt == 1	
	local mean_control=r(mean)
	ivreg2 `var' treatment final_stratum_id1-final_stratum_id13 entry_survey if post_exp == 1 & first_attempt == 1, small cl(uid_cluster)   
	quietly outreg2 treatment using "Results/Paper/Table7_PanelB.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control', "Sidak-Holm p-val", ``var'_p1') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)
	
}

* Wild cluster bootstrap or pairs cluster bootstrap: Table C5, Panel B, and Table D5, Panel B

preserve

keep if post_exp == 1 & first_attempt == 1

* wild cluster bootstrap

log using "Results/Appendix/TableC5_PanelB.log", replace
foreach var in verified_patient notcompl_refused notcompl_notfound notcompl_counswarning notcompl_died notcompl_other{
wildbootstrap regress `var' treatment final_stratum_id1-final_stratum_id13 entry_survey, cluster(uid_cluster) reps(5000) rseed(13915183)
}
log close

* pairs cluster bootstrap

log using "Results/Appendix/TableD5_PanelB.log", replace
foreach var in verified_patient notcompl_refused notcompl_notfound notcompl_counswarning notcompl_died notcompl_other{
clustse regress `var' treatment final_stratum_id1-final_stratum_id13 entry_survey, cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)
}
log close

restore


** Impact on the fraction of patients reported in the government registers which are verified

gen patient_found = (out_come_verify ~= -666) if out_come_verify ~= .

* Baseline regressions for patients who started the treatment before the start of the experiment: Table A10, Panel B

su verified_patient if treatment == 0 & post_exp == 0 & first_attempt == 1 & patient_found == 1
local mean_control=r(mean)
ivreg2 verified_patient treatment final_stratum_id1-final_stratum_id13 if post_exp == 0 & first_attempt == 1 & patient_found == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableA10_PanelB.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

* Impact regressions for patients who started the treatment after the start of the experiment: Table 7, Panel C

su verified_patient if treatment == 0 & post_exp == 1 & first_attempt == 1 & patient_found == 1
local mean_control=r(mean)
ivreg2 verified_patient treatment final_stratum_id1-final_stratum_id13 if post_exp == 1 & first_attempt == 1 & patient_found == 1, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table7_PanelC.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

* Wild cluster bootstrap or pairs cluster bootstrap: Table C5, Panel C, and Table D5, Panel C

preserve

keep if post_exp == 1 & first_attempt == 1 & patient_found == 1

* wild cluster bootstrap

log using "Results/Appendix/TableC5_PanelC.log", replace
foreach var in verified_patient{
wildbootstrap regress `var' treatment final_stratum_id1-final_stratum_id13, cluster(uid_cluster) reps(5000) rseed(13915183)
}
log close

* pairs cluster bootstrap

log using "Results/Appendix/TableD5_PanelC.log", replace
foreach var in verified_patient{
clustse regress `var' treatment final_stratum_id1-final_stratum_id13, cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)
}
log close

restore


****************************************
*** PART-2 *** Estimating the treatment's impact on the true number of detections
****************************************

** Preparing the data

* Keeping only one observation per patient
keep if first_attempt == 1

* Generating variable to indicate when patient started treatment
gen MonthIntoExp = mofd(treatment_start_date) - expstartdate_m + 1
ta MonthIntoExp
drop if MonthIntoExp == 19

* Adding 16 to enable the reshape below (doesn't work with negative values)
replace MonthIntoExp = MonthIntoExp + 16

* Aggregating outcome at the center-month level
so UID_Center MonthIntoExp
by UID_Center MonthIntoExp: egen verified_tot = total(verified_patient)

* Keeping only one record per center per month
by UID_Center MonthIntoExp: gen temp = _n
keep if temp == 1

* Keeping only relevant variables for analysis
keep verified_tot UID_Center MonthIntoExp treatment final_stratum_id1-final_stratum_id13 uid_cluster expstartdate_m expenddate_m

* Reshaping to create full list of MonthsIntoExp for each center
reshape wide verified_tot, i(UID_Center) j(MonthIntoExp)
reshape long verified_tot, i(UID_Center) j(MonthIntoExp)

* Correcting MonthIntoExp for their true values
replace MonthIntoExp = MonthIntoExp - 16
drop if MonthIntoExp > (expenddate_m - expstartdate_m + 1)
drop if MonthIntoExp == expenddate_m - expstartdate_m + 1
// We drop the last month of the experiment because we were no longer surveying new patients then

* Merging datasets

merge 1:1 UID_Center MonthIntoExp using "Data/Intermediate/hw_roster_by_month.dta"
drop if _merge == 2

replace verified_tot = 0 if verified_tot == .

drop _merge

merge m:1 Unique_ID using "Data/Intermediate/hw_controls.dta"
drop _merge

global hw_controls = "b_age_couns b_jobs_bef_oa_couns male_couns gen_caste_couns hindu_couns twelveandbelow_couns tertiary_couns hhd_size_couns ownhouse_couns b_age_dum_couns b_jobs_bef_oa_dum_couns male_dum_couns gen_caste_dum_couns hindu_dum_couns twelveandbelow_dum_couns tertiary_dum_couns hhd_size_dum_couns ownhouse_dum_couns"

global hw_controls_2 = "b_jobs_bef_oa_couns b_age_couns gen_caste_couns caste_st_couns religion_oth_couns same_nbhd_couns gen_caste_dum_couns caste_st_dum_couns religion_oth_dum_couns b_jobs_bef_oa_dum_couns same_nbhd_dum_couns b_age_dum_couns"

* Defining baseline values of each variable at center level
bysort UID_Center: egen temp=mean(verified_tot) if MonthIntoExp>-4 & MonthIntoExp<=0
by UID_Center: egen b_verified_tot=max(temp)
drop temp


** Regressions: Table 5, Panel A

su verified_tot if treatment == 0 & MonthIntoExp > 0
local mean_control=r(mean)

ivreg2 verified_tot treatment final_stratum_id1-final_stratum_id13 b_verified_tot if MonthIntoExp > 0, small cl(uid_cluster)
quietly outreg2 treatment using "Results/Paper/Table5_PanelA.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 verified_tot treatment final_stratum_id1-final_stratum_id13 b_verified_tot $hw_controls if MonthIntoExp > 0, small cl(uid_cluster)
quietly outreg2 treatment using "Results/Paper/Table5_PanelA.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Health Worker Controls, Yes) adec(3) dec(3) keep(treatment)

* Wild cluster bootstrap or pairs cluster bootstrap: Table C3, Panel A, and Table D3, Panel A

preserve

keep if MonthIntoExp > 0

foreach var in b_age_ b_jobs_bef_oa_ male_ gen_caste_ hindu_ twelveandbelow_ tertiary_ hhd_size_ ownhouse_ b_age_dum_ b_jobs_bef_oa_dum_ male_dum_ gen_caste_dum_ hindu_dum_ twelveandbelow_dum_ tertiary_dum_ hhd_size_dum_ ownhouse_dum_ {
rename `var'couns `var'
}

global hw_controls_b = "b_age_ b_jobs_bef_oa_ male_ gen_caste_ hindu_ twelveandbelow_ tertiary_ hhd_size_ ownhouse_ b_age_dum_ b_jobs_bef_oa_dum_ male_dum_ gen_caste_dum_ hindu_dum_ twelveandbelow_dum_ tertiary_dum_ hhd_size_dum_ ownhouse_dum_"

* wild cluster bootstrap

log using "Results/Appendix/TableC3_PanelA.log", replace
wildbootstrap regress verified_tot treatment final_stratum_id1-final_stratum_id13 b_verified_tot, cluster(uid_cluster) reps(5000) rseed(13915183)
wildbootstrap regress verified_tot treatment final_stratum_id1-final_stratum_id13 b_verified_tot $hw_controls_b, cluster(uid_cluster) reps(5000) rseed(13915183)
log close

* pairs cluster bootstrap

log using "Results/Appendix/TableD3_PanelA.log", replace
clustse regress verified_tot treatment final_stratum_id1-final_stratum_id13 b_verified_tot , cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)
clustse regress verified_tot treatment final_stratum_id1-final_stratum_id13 b_verified_tot $hw_controls_b, cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)
log close

restore

* With second set of controls: Table E3

su verified_tot if treatment == 0 & MonthIntoExp > 0
local mean_control=r(mean)

ivreg2 verified_tot treatment final_stratum_id1-final_stratum_id13 b_verified_tot if MonthIntoExp > 0, small cl(uid_cluster)
quietly outreg2 treatment using "Results/Appendix/TableE3.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 verified_tot treatment final_stratum_id1-final_stratum_id13 b_verified_tot $hw_controls_2 if MonthIntoExp > 0, small cl(uid_cluster)
quietly outreg2 treatment using "Results/Appendix/TableE3.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Health Worker Controls, Yes) adec(3) dec(3) keep(treatment)


****************************************
*** PART-3 *** Estimating the treatment's impact on the quality of reporting on patient detection, center-level analysis
****************************************

** Generating dataset with number of verified detections

preserve

keep if MonthIntoExp > 0

keep UID_Center MonthIntoExp verified_tot b_verified_tot

rename verified_tot detection_survey
rename b_verified_tot b_detection_survey

save "Data/Intermediate/Table7_PanelA_1_input.dta", replace

restore


** Generating dataset with number of reported detections

use "Data/Original/centerwise_sheets.dta", clear

* Replace Unique_ID in the center * months for which it is missing
drop Unique_ID
merge m:1 UID_Center MonthIntoExp using "Data/Intermediate/hw_roster_by_month.dta"
ta _merge
ta MonthIntoExp if _merge == 1
drop if _merge == 2
drop _merge

* Define alternative outcomes, at the center level, not center x area level, for merge with patient data (since we don't know which mobile area a patient went to in the patient data)

so UID_Center MonthIntoExp
bysort UID_Center MonthIntoExp: egen detection_oa = total(NewPatients)

bysort UID_Center MonthIntoExp: gen id = _n
bysort UID_Center: egen temp=mean(detection_oa) if MonthIntoExp>=101 & MonthIntoExp~=. & id == 1
by UID_Center: egen b_detection_oa=max(temp)
keep if id == 1
drop id temp

keep if MonthIntoExp<101 & MonthIntoExp ~= .

keep UID_Center Unique_ID MonthIntoExp detection_oa b_detection_oa

save "Data/Intermediate/Table7_PanelA_2_input.dta", replace


** Comparing impact on detections measured using multiple data sources

* Main specification: Table 7, Panel A

use "Data/Intermediate/Table7_PanelA_2_input.dta", clear

merge 1:1 UID_Center MonthIntoExp using "Data/Intermediate/Table7_PanelA_1_input.dta"
ta _merge
drop _merge

merge m:1 Unique_ID UID_Center using "Data/Original/hw_roster.dta"
ta _merge
drop _merge

merge m:1 Unique_ID using "Data/Intermediate/hw_controls.dta"
ta _merge
drop _merge

global hw_controls = "b_age_couns b_jobs_bef_oa_couns male_couns gen_caste_couns hindu_couns twelveandbelow_couns tertiary_couns hhd_size_couns ownhouse_couns b_age_dum_couns b_jobs_bef_oa_dum_couns male_dum_couns gen_caste_dum_couns hindu_dum_couns twelveandbelow_dum_couns tertiary_dum_couns hhd_size_dum_couns ownhouse_dum_couns"

global hw_controls_2 = "b_jobs_bef_oa_couns b_age_couns gen_caste_couns caste_st_couns religion_oth_couns same_nbhd_couns gen_caste_dum_couns caste_st_dum_couns religion_oth_dum_couns b_jobs_bef_oa_dum_couns same_nbhd_dum_couns b_age_dum_couns"

gen detection_diff_oa = detection_oa - detection_survey
gen b_detection_diff_oa = b_detection_oa - b_detection_survey

su detection_survey if treatment == 0
local mean_control=r(mean)

ivreg2 detection_survey treatment final_stratum_id1-final_stratum_id13 b_detection_survey, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table7_PanelA.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 detection_survey treatment final_stratum_id1-final_stratum_id13 b_detection_survey $hw_controls, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table7_PanelA.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Health worker controls, Yes) adec(3) dec(3) keep(treatment)

su detection_oa if treatment == 0
local mean_control=r(mean)

ivreg2 detection_oa treatment final_stratum_id1-final_stratum_id13 b_detection_oa, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table7_PanelA.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 detection_oa treatment final_stratum_id1-final_stratum_id13 b_detection_oa $hw_controls, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table7_PanelA.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Health worker controls, Yes) adec(3) dec(3) keep(treatment)

su detection_diff_oa if treatment == 0
local mean_control=r(mean)

ivreg2 detection_diff_oa treatment final_stratum_id1-final_stratum_id13 b_detection_diff_oa, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table7_PanelA.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 detection_diff_oa treatment final_stratum_id1-final_stratum_id13 b_detection_diff_oa $hw_controls, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Paper/Table7_PanelA.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Health worker controls, Yes) adec(3) dec(3) keep(treatment)

* Wild cluster bootstrap or pairs cluster bootstrap: Table C5, Panel A, and Table D5, Panel A

preserve

foreach var in b_age_ b_jobs_bef_oa_ male_ gen_caste_ hindu_ twelveandbelow_ tertiary_ hhd_size_ ownhouse_ b_age_dum_ b_jobs_bef_oa_dum_ male_dum_ gen_caste_dum_ hindu_dum_ twelveandbelow_dum_ tertiary_dum_ hhd_size_dum_ ownhouse_dum_ {
rename `var'couns `var'
}

global hw_controls_b = "b_age_ b_jobs_bef_oa_ male_ gen_caste_ hindu_ twelveandbelow_ tertiary_ hhd_size_ ownhouse_ b_age_dum_ b_jobs_bef_oa_dum_ male_dum_ gen_caste_dum_ hindu_dum_ twelveandbelow_dum_ tertiary_dum_ hhd_size_dum_ ownhouse_dum_"

* wild cluster bootstrap

log using "Results/Appendix/TableC5_PanelA.log", replace

wildbootstrap regress detection_survey treatment final_stratum_id1-final_stratum_id13 b_detection_survey, cluster(uid_cluster) reps(5000) rseed(13915183)   
wildbootstrap regress detection_survey treatment final_stratum_id1-final_stratum_id13 b_detection_survey $hw_controls_b, cluster(uid_cluster) reps(5000) rseed(13915183)   

wildbootstrap regress detection_oa treatment final_stratum_id1-final_stratum_id13 b_detection_oa, cluster(uid_cluster) reps(5000) rseed(13915183)   
wildbootstrap regress detection_oa treatment final_stratum_id1-final_stratum_id13 b_detection_oa $hw_controls_b, cluster(uid_cluster) reps(5000) rseed(13915183)   

wildbootstrap regress detection_diff_oa treatment final_stratum_id1-final_stratum_id13 b_detection_diff_oa, cluster(uid_cluster) reps(5000) rseed(13915183)   
wildbootstrap regress detection_diff_oa treatment final_stratum_id1-final_stratum_id13 b_detection_diff_oa $hw_controls_b, cluster(uid_cluster) reps(5000) rseed(13915183)   

log close

* pairs cluster bootstrap

log using "Results/Appendix/TableD5_PanelA.log", replace

clustse regress detection_survey treatment final_stratum_id1-final_stratum_id13 b_detection_survey, cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)   
clustse regress detection_survey treatment final_stratum_id1-final_stratum_id13 b_detection_survey $hw_controls_b, cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)   

clustse regress detection_oa treatment final_stratum_id1-final_stratum_id13 b_detection_oa, cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)   
clustse regress detection_oa treatment final_stratum_id1-final_stratum_id13 b_detection_oa $hw_controls_b, cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)   

clustse regress detection_diff_oa treatment final_stratum_id1-final_stratum_id13 b_detection_diff_oa, cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)   
clustse regress detection_diff_oa treatment final_stratum_id1-final_stratum_id13 b_detection_diff_oa $hw_controls_b, cluster(uid_cluster) method(pairs) reps(5000) seed(13915183)   

log close

restore

* With second set of controls: Table E5

su detection_survey if treatment == 0
local mean_control=r(mean)

ivreg2 detection_survey treatment final_stratum_id1-final_stratum_id13 b_detection_survey, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableE5.out", replace nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 detection_survey treatment final_stratum_id1-final_stratum_id13 b_detection_survey $hw_controls_2, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableE5.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Health worker controls, Yes) adec(3) dec(3) keep(treatment)

su detection_oa if treatment == 0
local mean_control=r(mean)

ivreg2 detection_oa treatment final_stratum_id1-final_stratum_id13 b_detection_oa, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableE5.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 detection_oa treatment final_stratum_id1-final_stratum_id13 b_detection_oa $hw_controls_2, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableE5.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Health worker controls, Yes) adec(3) dec(3) keep(treatment)

su detection_diff_oa if treatment == 0
local mean_control=r(mean)

ivreg2 detection_diff_oa treatment final_stratum_id1-final_stratum_id13 b_detection_diff_oa, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableE5.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes) adec(3) dec(3) keep(treatment)

ivreg2 detection_diff_oa treatment final_stratum_id1-final_stratum_id13 b_detection_diff_oa $hw_controls_2, small cl(uid_cluster)   
quietly outreg2 treatment using "Results/Appendix/TableE5.out", append nolabel asterisk(se) nocons nonote se addstat("Mean in Control Group", `mean_control') addtext(Strata fixed effects, Yes, Health worker controls, Yes) adec(3) dec(3) keep(treatment)
